﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AiXiu.Model;

namespace AiXiu.DAL
{
    public class VideoDAL
    {
        AiXiuDbContext dbcontext = new AiXiuDbContext();
        /// <summary>
        /// 上传视频
        /// </summary>
        /// <param name="video"></param>
        /// <returns></returns>
        public TBVideos AddVideo(TBVideos video)
        {
            video.UploadTime = DateTime.Now;
            dbcontext.Entry(video).State = EntityState.Added;
            dbcontext.SaveChanges();
            return video;
        }
        /// <summary>
        /// 获取处理中的视频ID
        /// </summary>
        /// <returns></returns>
        public List<string> GetVideoId()
        {
            //获取处理中的视频ID列表(上传中、上传成功、转码中)
            List<TBVideos> list = dbcontext.TBVideos.Where(v => v.Status == (int)VideoStatus.Uploading
            || v.Status == (int)VideoStatus.UploadSucces || v.Status == (int)VideoStatus.Transcoding).ToList();

            List<string> videoId = list.Select(v => v.VideoId).ToList();
            return videoId;
        }
        /// <summary>
        /// 更新视频上传状态码，视频URL
        /// </summary>
        /// <param name="videos"></param>
        public void UpdateVideos(List<TBVideos> videos)
        {
            foreach (TBVideos v in videos)
            {
                //通过视频id找到该条数据
                TBVideos tBVideos = dbcontext.TBVideos.Find(v.VideoId);
                //判断 地址栏是否为空，不为空，更新地址栏到数据库中，截取指定字符串
                if (!string.IsNullOrEmpty(v.CoverURL))
                {
                    tBVideos.CoverURL = v.CoverURL.Substring(0, v.CoverURL.IndexOf('?'));
                }
                //更新状态  保存到数据库中
                tBVideos.Status = v.Status;
                dbcontext.Entry(tBVideos).State = EntityState.Modified;
                dbcontext.SaveChanges();
            }
        }
        /// <summary>
        /// 视频列表显示
        /// </summary>
        /// <returns></returns>
        public List<TBVideos> GetVideos()
        {
            return (List<TBVideos>)dbcontext.TBVideos
                 .Where(v => v.Status == (int)VideoStatus.Normal)
                 .Include(v => v.TBUsers) //链接查询用户信息表
                 .OrderByDescending(v => v.UploadTime)
                 .ToList();
        }
        /// <summary>
        /// 通过视频Id获取视频信息
        /// </summary>
        /// <param name="videoId"></param>
        /// <returns></returns>
        public TBVideos GetVideoByVideoId(string videoId)
        {
            TBVideos video = dbcontext.TBVideos
                 .Include(m => m.TBUsers)
                 .SingleOrDefault(m => m.VideoId == videoId);
            return video;
            //return dbcontext.TBVideos.Find(videoId);
        }

        /// <summary>
        /// 通过用户Id获取视频列表
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<TBVideos> GetVideosByUserId(int userId)
        {
            return dbcontext.TBVideos
                .Where(m => m.UserId == userId)
                .OrderByDescending(m => m.UploadTime)
                .ToList();
        }
        /// <summary>
        /// 根据视频Id删除视频
        /// </summary>
        /// <param name="videoId"></param>
        /// <returns></returns>
        public int DeleteVideo(string videoId)
        {
            TBVideos video = dbcontext.TBVideos.Find(videoId);
            dbcontext.TBVideos.Remove(video);
            int result = dbcontext.SaveChanges();
            return result;
        }
    }
}
